package cn.cas.mango.util.paramCheck;

import cn.cas.mango.dto.param.HotListDetailParam;
import cn.cas.mango.service.ConfigService;
import cn.cas.mango.util.RenderDateUtil;
import cn.cas.mango.util.string.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
@Slf4j
public class HotListDetailParamCheckUtil {
  @Autowired
  private ConfigService configService;
  public void check(HotListDetailParam param) {
    if (param.getStartDate() == null) {
      String today = RenderDateUtil.getToday();
      param.setStartDate(today + " 00:00:00");
    }
    if (param.getEndDate() == null) {
      param.setEndDate(RenderDateUtil.getNow());
    }
    String startDate = param.getStartDate();
    String endDate = param.getEndDate();

    if (!param.getStartDate().contains(" ")) {
      startDate = param.getStartDate() + " 00:00:00";
    }
    if (!param.getEndDate().contains(" ")) {
      endDate = RenderDateUtil.plus1day(param.getEndDate()) + " 00:00:00";
    }
    param.setStartDate(startDate);
    param.setEndDate(endDate);
    if (param.getFrom() == null) {
      param.setFrom(0);
    }
    if (param.getPageSize() == null) {
      param.setPageSize(configService.getPageSize());
    }
    if (param.getLikeDegree() == null) {
      param.setLikeDegree(configService.getLikeDegree());
    }
    String uuid = StringUtils.getUUID();
    param.setUuid(uuid);
    if (param.getAggWeight() == null) {
      param.setAggWeight(0.3 * configService.getAggTimes());
    } else {
      param.setAggWeight(param.getAggWeight() * configService.getAggTimes());
    }
    if (param.getMaxReadingSize() == null) {
      param.setMaxReadingSize(configService.getMaxReadSize());
    }
    if (Strings.isBlank(param.getPlatformCode())) {
      param.setPlatformCode("all");
    }
    param.setPlatformList(Arrays.asList(param.getPlatformCode().split(",")));
    if (Strings.isBlank(param.getSubject())) {
      param.setSubject("all");
    }
    param.setSubjectList(Arrays.asList(param.getSubject().split(",")));
  }
}
